package com.hlz.fastnote

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.Gravity
import android.view.View
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import androidx.drawerlayout.widget.DrawerLayout
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_main.*
import org.litepal.LitePal
import java.util.*

class MainActivity : AppCompatActivity() {

    //#bd8cbb
    var s = 5
    val timer by lazy { Timer() }
    var isStart = true

    val adapter by lazy { LeftAdapter(this,mDrawerLayout) }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setTitle("")
        setSupportActionBar(mToolbar)

        left.setOnClickListener { }

        initLeft()

        ivMenu.setOnClickListener {
            mDrawerLayout.openDrawer(Gravity.LEFT)
        }

        tvSave.setOnClickListener {
            submit(etNote)
        }

        etNote.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                s = 5
                tvCount.text = "字数：${p0.countV2()}"
            }

            override fun afterTextChanged(p0: Editable?) {

            }
        })



        timer.schedule(object : TimerTask() {
            override fun run() {
                if (!isStart) return
                if (s <= 0) {
                    s = 0
                    etNote.post {
                        etNote.setText("")
                    }
                }
                tvCountDown.post { tvCountDown.text = "${s}s" }
                s--
            }
        }, 0, 1000)

    }

    private fun initLeft() {

        mRecyclerView.layoutManager = LinearLayoutManager(this)
        mRecyclerView.adapter = adapter
        mDrawerLayout.addDrawerListener(object : DrawerLayout.DrawerListener {

            override fun onDrawerSlide(drawerView: View, slideOffset: Float) {

            }


            override fun onDrawerOpened(drawerView: View) {
                val notes = LitePal.findAll(Note::class.java)
                if (!notes.isNullOrEmpty()) {
                    adapter.setList(notes.apply { reverse() })
                    tvNoData.visibility = View.GONE
                } else {
                    tvNoData.visibility = View.VISIBLE
                }
            }

            override fun onDrawerClosed(drawerView: View) {

            }

            override fun onDrawerStateChanged(newState: Int) {

            }
        })

    }

    fun stop() {
        isStart = false
    }

    fun resume() {
        isStart = true
    }

    private fun submit(etNote: EditText) {
        val c = etNote.text.toString().trim()
        if (c.isNullOrEmpty()) {
            ToastUtil.showToast("你还未输入内容")
            return
        }

        val dialog = MyDialog(this)
        dialog.setOnDialogListener(object : MyDialog.OnDialogListener {
            override fun save(text: String) {
                val note = Note()
                note.content = c
                note.time = System.currentTimeMillis()
                note.title = text
                val s = note.save()
                if (s) {
                    ToastUtil.showToast("保存成功")
                } else {
                    ToastUtil.showToast("保存失败")
                }
            }

            override fun dismiss() {
                resume()
            }

            override fun show() {
                stop()
            }
        })

        dialog.show()

    }

    override fun onDestroy() {
        super.onDestroy()
        timer.cancel()
    }
}